package blog.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import blog.entity.Menu;
import blog.service.MenuService;

@Controller
@RequestMapping("/menu")
public class MenuController {
	@Resource
	MenuService menuService;

	@RequestMapping("")
	public String index(ModelMap m) {
		List<Menu> menuList = menuService.findAll();
		m.put("menuList", menuList);
		return "Menu/menu-list";
	}

	/**
	 * 删除菜单
	 */
	@RequestMapping("/delete/{menuId}")
	public String delete(@PathVariable("menuId") Integer menuId) {
		menuService.deleteMenuById(menuId);
		return "forward:/menu";
	}

	/**
	 * 菜单添加跳转
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Menu menu) {
		if (menu.getMenuOrder() == null) {
			menu.setMenuOrder(1);
		}
		menuService.add(menu);
		return "forward:/menu";
	}

}
